package com.br3akr.rollerTransferProtocol.transferProtocol;

import java.io.Serializable;

public class FromClient implements Serializable {
	private static final long serialVersionUID = 1L;

	public enum ActionType {
		CONNECT (String.class, Boolean.class),
		ROLL (),
		MESSAGE (String.class),
		GRANT_FP (Integer.class, String.class),
		TYPED (),
		CLEARED_TEXT (),
		SPEND_FP ();
		
		private final Class<?>[] params;
		
		ActionType(Class<?>... args) {
			int i = 0;
			params = new Class[args.length];
			for (Class<?> c : args) {
				params[i++] = c;
			}
		}
		
		public Class<?>[] getParameters() {
			return params;
		}
	}
	private Object[] actionParams;
	private ActionType actionType;
	
	public FromClient(ActionType at, Object[] parameters) {
		this.actionType = at;
		StringBuilder actionParamsSB = new StringBuilder();
		StringBuilder providedParamsSB = new StringBuilder();
		
		for (Class<?> c : actionType.getParameters()) {
			if (actionParamsSB.toString().trim().length() != 0) {
				actionParamsSB.append(", ");
			}
			actionParamsSB.append(c.getName());
		}
		
		for (Object o : parameters) {
			Class<? extends Object> c = o.getClass();
			if (providedParamsSB.toString().trim().length() != 0) {
				providedParamsSB.append(", ");
			}
			providedParamsSB.append(c.getName());
		}
		
		if (!actionParamsSB.toString().equals(providedParamsSB.toString())) {
			throw new IllegalArgumentException("Provided parameters (" 
					+ providedParamsSB.toString() 
					+ ") does not match expected parameters (" 
					+ actionParamsSB.toString() + ").");
		}
		
		actionParams = parameters;
	}
	
	public ActionType getActionType() {
		return actionType;
	}
	
	public Object[] getActionParameters() {
		return actionParams;
	}
}